﻿$(function () {
    var resizeBody = function () {
        var body_height = $(window).height();
        $('#bodyer').height(body_height - 120);
        $('#bodyer').css({
            'overflow': 'auto'
        });
    };
    resizeBody();
    $(window).resize(function () {
        resizeBody();
    });
});


//url helper
jQuery.UrlHelper = {
    getObject: function (urlserialize) {
        if (urlserialize) {
            urlserialize = decodeURI(urlserialize);
            var kvs = $.map((urlserialize).split('&'), function (e, i) {
                var kv = (e + '').split('=');
                return { key: kv[0], value: kv[1] };
            });
            var params = {};
            for (var key in kvs) {
                var _key = kvs[key].key;
                var value = null;
                if (typeof (params[_key]) == 'undefined') {
                    if ($.grep(kvs, function (e, i) { return e.key == _key; }).length > 1) {
                        value = [];
                        value.push(kvs[key].value);
                        params[_key] = value;
                    } else {
                        value = kvs[key].value;
                        params[_key] = value;
                    }
                } else if (typeof (params[_key]) == 'object') {
                    value = params[_key] ? params[_key] : [];
                    value.push(kvs[key].value);
                    params[_key] = value;
                } else {
                    value = kvs[key].value;
                    params[_key] = value;
                }
            }
            var resultParams = {};
            var objectParams = [];
            for (var key in params) {
                if ((key.indexOf('.') > -1)) {
                    var _key = (key.split('.'))[0];
                    if ((key == (_key + '.index')) && key.indexOf('[') < 0) {
                        objectParams.push(_key);
                    }
                } else {
                    resultParams[key] = params[key];
                }
            }
            for (var i = 0; i < objectParams.length; i++) {
                var key = objectParams[i];
                var value = [];
                var keys = params[key + '.index'];
                if (keys) {
                    for (var j = 0; j < keys.length; j++) {
                        var props = [];
                        for (var prop in params) {
                            var keyprev = key + '[' + keys[j] + ']'
                            if (prop.indexOf(keyprev) > -1) {
                                props.push(prop.split('.')[1]);
                            }
                        }
                        var cvalue = {};
                        for (var k = 0; k < props.length; k++) {
                            var okey = (key + '[' + keys[j] + '].') + props[k];
                            cvalue[props[k]] = params[okey]
                        }
                        value.push(cvalue);
                    }
                }
                resultParams[key] = value;
            }
            return resultParams;
        }
        return null;
    },
    getParams: function (url) {
        if (url) {
            url = decodeURI(url);
            var local = url.split('?')[0];
            var kvs = url.split('?').length == 1 ? [] : $.map((url.split('?')[1]).split('&'), function (e, i) {
                var kv = (e + '').split('=');
                return { key: kv[0], value: kv[1] };
            });
            var params = {};
            for (var key in kvs) {
                var _key = kvs[key].key;
                var value = null;
                if (typeof (params[_key]) == 'undefined') {
                    if ($.grep(kvs, function (e, i) { return e.key == _key; }).length > 1) {
                        value = [];
                        value.push(kvs[key].value);
                        params[_key] = value;
                    } else {
                        value = kvs[key].value;
                        params[_key] = value;
                    }
                } else if (typeof (params[_key]) == 'object') {
                    value = params[_key] ? params[_key] : [];
                    value.push(kvs[key].value);
                    params[_key] = value;
                } else {
                    value = kvs[key].value;
                    params[_key] = value;
                }
            }
            return { local: local, params: params };
        }
        return null;
    },
    setParam: function (url, key, value) {
        var params = this.getParams(url);
        if (params) {
            var pdatas = params.params;
            var local = params.local;
            pdatas[key] = value;
            return local + '?' + $.param(pdatas);
        }
        return url;
    },
    addParam: function (url, key, value) {
        var params = this.getParams(url);
        if (params) {
            var pdatas = params.params;
            var local = params.local;
            if (typeof (pdatas[key]) == 'undefined') {
                pdatas[key] = value;
            } else if (typeof (pdatas[key]) == 'object') {
                var ovalue = pdatas[key] ? pdatas[key] : [];
                ovalue.push(value);
                pdatas[key] = ovalue;
            } else {
                var ovalue = [pdatas[key]];
                ovalue.push(value);
                pdatas[key] = ovalue;
            }
            return local + '?' + $.param(pdatas);
        }
        return url;
    },
    removeParam: function (url, key) {
        var params = this.getParams(url);
        if (params) {
            var pdatas = params.params;
            var local = params.local;
            pdatas[key] = null;
            return local + '?' + $.param(pdatas);
        }
        return url;
    }
};

jQuery.CONST = {
    emptyId: '00000000-0000-0000-0000-000000000000',
    words: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
};

(function ($) {
    $.fn.StandardSelect = function (options) {
        var defaults = {
            source: [],
            value: ''
        };
        $.extend(defaults, options);
        options = defaults;
        var $obj = $(this);
        var _setOptions = function () {
            $obj.html('');
            $obj.append('<option value="">--请选择--</option>');
            $.each(options.source, function (index, elm) {
                var $option = $('<option value="' + elm.Value + '"' + (elm.Value == options.value ? " selected" : "") + '>' + (elm.ShortText ? elm.ShortText : elm.Text) + '</option>');
                $obj.append($option);
            });
        };
        _setOptions();
    };
    $.fn.GBT2260Dialog = function (options) {
        var defaults = {
            value: '',
            title: ''
        };
        $.extend(defaults, options);
        options = defaults;
        var $elm = $(this);
        var showId = $elm.attr('id');
        var valueId = showId.split('-')[0];
        var $valueElm = $('#' + valueId);
        options.value =options.value?options.value: $valueElm.val();
        var $dlg = $('#' + valueId + '-dlg').ch2Dialog({
            modal: true,
            show: false,
            title: options.title,
            buttons: [
                {
                    text: '清空',
                    iconClassName: 'fa-trash',
                    className: 'u-btn-blue',
                    handler: function (b, m) {
                        $elm.val('');
                        $valueElm.val('');
                        m.show(false);
                    }
                },
                {
                    text: '关闭',
                    iconClassName: 'fa-times-circle',
                    className: 'u-btn-yellow',
                    handler: function (b, m) {
                        m.show(false);
                    }
                }
            ]
        });
        var source = getStandards('GBT2260');
        source = $.grep(source, function (e, i) {
            return e.Text != '县' && e.Text != '市' && e.Text != '区';
        });

        var setDefaultValue=function(){
            var item=$.grep(source,function(e,i){
                return e.Value==options.value;
            });
            var ivText="";
            if (item.length) {
                ivText=item[0].ShortText?item[0].ShortText:item[0].Text;
            };
            $elm.val(ivText);
            $valueElm.val(options.value);
        };
        setDefaultValue();
        var setList = function (showItem, data) {
            var $optionList = $dlg.elm.find('.option-list');
            $optionList.empty();
            $optionList.data('items', data);
            showItem = showItem ? showItem : data;
            var showValues = $.map(showItem, function (e, i) { return e.Value; });
            $.each(data, function (index, e) {
                if ($.inArray(e.Value, showValues) > -1) {
                    var $li = $('<li><a href="javascript:void(0);" data-value="' + e.Value + '">' + (e.ShortText ? e.ShortText : e.Text) + '</a></li>');
                    $li.data('item', e);
                    if (e.Value == options.value) {
                        $li.addClass('active');
                    }
                    $li.appendTo($optionList);
                }
            });
        };
        var show = function () {
            var $layoutHtml = $('<div class="_2260-body"></div>');
            $layoutHtml.append('<div class="s-form"><span>关键字：</span><input class="u-input" type="text" name="kw" /></div>');
            $layoutHtml.append('<ul class="option-list"></ul>');
            $dlg.show(true).loadContent(false, $layoutHtml);
            setList(null, source);
        };
        $elm.on('click', function () {
            show();
        });
        $dlg.elm.on('click', '.option-list a', function () {
            $valueElm.val($(this).attr('data-value'));
            $elm.val($(this).text());
            $dlg.show(false);
        });
        $dlg.elm.on('keyup', '[name="kw"]', function () {
            var kw = $(this).val();
            var showis = $.grep(source, function (e, i) {
                var Pinyin = e.Pinyin ? e.Pinyin : '';
                var PinyinWithSpace = e.PinyinWithSpace ? e.PinyinWithSpace : '';
                var ShortPinyin = e.ShortPinyin ? e.ShortPinyin : '';
                var Text = e.Text ? e.Text : '';
                var ShortText = e.ShortText ? e.ShortText : '';
                return ((Pinyin.indexOf(kw) > -1) || (PinyinWithSpace.indexOf(kw) > -1) || (ShortPinyin.indexOf(kw) > -1) || (Text.indexOf(kw) > -1) || (ShortText.indexOf(kw) > -1));
            });
            setList(showis,source);
        });
    };
    $.fn.StandardDialog = function (options) {
        var defaults = {
            code:'',
            value: '',
            title: '',
            canself:false
        };
        $.extend(defaults, options);
        options = defaults;
        var $elm = $(this);
        var showId = $elm.attr('id');
        var valueId = showId.split('-')[0];
        var $valueElm = $('#' + valueId);
        options.value =options.value?options.value: $valueElm.val();
        var $dlg = $('#' + valueId + '-dlg').ch2Dialog({
            modal: true,
            show: false,
            title: options.title,
            buttons: [
                {
                    text: '清空',
                    iconClassName: 'fa-trash',
                    className: 'u-btn-blue',
                    handler: function (b, m) {
                        $elm.val('');
                        $valueElm.val('');
                        m.show(false);
                    }
                },
                {
                    text: '关闭',
                    iconClassName: 'fa-times-circle',
                    className: 'u-btn-yellow',
                    handler: function (b, m) {
                        m.show(false);
                    }
                }
            ]
        });
        var source = getStandards(options.code);
        var setDefaultValue=function(){
            var item=$.grep(source,function(e,i){
                return e.Value==options.value;
            });
            var ivText=options.value;
            if (item.length) {
                ivText=item[0].ShortText?item[0].ShortText:item[0].Text;
            };
            $elm.val(ivText);
            $valueElm.val(options.value);
            
        };
        setDefaultValue();
        var setList = function (showItem, data) {
            var $optionList = $dlg.elm.find('.option-list');
            $optionList.empty();
            $optionList.data('items', data);
            showItem = showItem ? showItem : data;
            var showValues = $.map(showItem, function (e, i) { return e.Value; });
            $.each(data, function (index, e) {
                if ($.inArray(e.Value, showValues) > -1) {
                    var $li = $('<li><a href="javascript:void(0);" data-value="' + e.Value + '">' + (e.ShortText ? e.ShortText : e.Text) + '</a></li>');
                    $li.data('item', e);
                    if (e.Value == options.value) {
                        $li.addClass('active');
                    }
                    $li.appendTo($optionList);
                }
            });
        };
        var show = function () {
            var $layoutHtml = $('<div class="_2260-body"></div>');
            $layoutHtml.append('<div class="s-form"><span>关键字：</span><input class="u-input" type="text" name="kw" /></div>');
            $layoutHtml.append('<ul class="option-list"></ul>');
            if (options.canself) {
                $layoutHtml.append('<div class="self-form form-inline"><span>自定义：</span><input class="u-input" type="text" name="svalue" />&nbsp;<button type="button" class="u-btn u-btn-blue u-btn-sm svalue-btn">确认</button></div>');
            }
            $dlg.show(true).loadContent(false, $layoutHtml);
            setList(null, source);
        };
        $elm.on('click', function () {
            show();
        });
        $dlg.elm.on('click', '.option-list a', function () {
            $valueElm.val($(this).attr('data-value'));
            $elm.val($(this).text());
            $dlg.show(false);
        });
        $dlg.elm.on('click', '.svalue-btn', function () {
            if (options.canself&&$dlg.elm.find('[name="svalue"]').val()) {
                var selfValue=$dlg.elm.find('[name="svalue"]').val();
                $valueElm.val(selfValue);
                $elm.val(selfValue);
            }
            $dlg.show(false);
        });
        $dlg.elm.on('keyup', '[name="kw"]', function () {
            var kw = $(this).val();
            var showis = $.grep(source, function (e, i) {
                var Pinyin = e.Pinyin ? e.Pinyin : '';
                var PinyinWithSpace = e.PinyinWithSpace ? e.PinyinWithSpace : '';
                var ShortPinyin = e.ShortPinyin ? e.ShortPinyin : '';
                var Text = e.Text ? e.Text : '';
                var ShortText = e.ShortText ? e.ShortText : '';
                return ((Pinyin.indexOf(kw) > -1) || (PinyinWithSpace.indexOf(kw) > -1) || (ShortPinyin.indexOf(kw) > -1) || (Text.indexOf(kw) > -1) || (ShortText.indexOf(kw) > -1));
            });
            setList(showis,source);
        });
    };
})(jQuery);